<?php

/**
 * Contains the paginate function to create pagination HTML based on parameters.
 *
 * PHP Version 5
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */

/** @uses encode For safe handling of suffix values. */
require_once 'function_encode.php';

/** @uses valueOf For safe handling of arrays. */
require_once 'function_valueof.php';
 
/**
 * Pagination function for creating pagination HTML based on parameters.
 *
 * @param int $page    The current page view.
 * @param int $total   The total number of items.
 * @param int $perPage The number of items per page.
 *
 * @access public
 * @return string
 */
function paginate($page, $total, $perPage)
{
    /** Initializes variables and calculates limit values. */
    $pages = ceil($total / $perPage);
    $first = 1;
    $last = $pages;
    $prev = max($first, $page - 1);
    $next = min($last, $page + 1);
    
    /** Determines if pagination needs to be created or not. */
    if ($pages <= 1) {
        return '';
    }
    
    /** Builds the suffix based on the GET values. */
    $suffix = '';
    $getArrayKeys = array_keys($_GET);
    for ($i = 0; $i < count($getArrayKeys); $i++) {
        if (strcasecmp($getArrayKeys[$i], 'page' == 0)) {
            continue;
        }
        $getVar = valueOf($_GET, $getArrayKeys[$i]);
        $suffix .= '&amp;' . encode($getArrayKeys[$i]) . '=' . encode($getVar);
    }
    
    /** Calculates range values for the numbers in the middle. */
    if (($last - $first) >= 9) {
        $rangeMin = max($page - 4, $first);
        $rangeMax = min($page + 4, $last);
        while (($rangeMax - $rangeMin) < 8) {
            $rangeMin = max($rangeMin - 1, $first);
            $rangeMax = min($rangeMax + 1, $last);
        }
    } else {
        $rangeMin = $first;
        $rangeMax = $last;
    }
    
    /** Constructs and returns HTML. */
    $html = ' <div class="pagination">' . "\n";
    if ($page != $first) {
        $html .= '  <a href="?page=' . $first . $suffix . '">First</a>' . "\n";
    } else {
        $html .= '  <strong>First</strong>' . "\n";
    }
    if ($page != $prev) {
        $html .= '  <a href="?page=' . $prev . $suffix . '">Previous</a>' . "\n";
    } else {
        $html .= '  <strong>Next</strong>' . "\n";
    }
    for ($i = $rangeMin; $i <= $rangeMax; $i++) {
        if ($page != $i) {
            $html .= '  <a href="?page=' . $i . $suffix . '">' . $i . '</a>' . "\n";
        } else {
            $html .= '  <strong>' . $i . '</strong>' . "\n";
        }
    }
    if ($page != $next) {
        $html .= '  <a href="?page=' . $next . $suffix . '">Next</a>' . "\n";
    } else {
        $html .= '  <strong>Next</strong>' . "\n";
    }
    if ($page != $last) {
        $html .= '  <a href="?page=' . $last . $suffix . '">Last</a>' . "\n";
    } else {
        $html .= '  <strong>Last</strong>' . "\n";
    }
    $html .= ' </div>' . "\n";
    return $html;
}

?>